package com.samp.bblock.seckill.controller;

import com.samp.bblock.core.base.BblockResponse;
import com.samp.bblock.seckill.dto.SeckillDTO;
import com.samp.bblock.seckill.service.SeckillService;
import com.samp.bblock.util.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class SeckillController {
	
	@Resource
    private SeckillService seckillService;

//	private static final Logger logger = LoggerFactory.getLogger(SeckillController.class);

	@SuppressWarnings("rawtypes")
	@RequestMapping("/seckill/doSeckill.do")
	@ResponseBody
	public BblockResponse testTransaction(@RequestParam(name="seckillId", required=false) String seckillId,
										  @RequestParam(name="userName", required=false) String userName) {
		if (StringUtils.isEmpty(seckillId)) {
			return new BblockResponse(Constants.RESPONSE_CODE_PARAM_ERROR,"参数seckillId不能为空");
		}
		if (StringUtils.isEmpty(userName)) {
			return new BblockResponse(Constants.RESPONSE_CODE_PARAM_ERROR,"参数userName不能为空");
		}
		try {
			SeckillDTO seckillDTO = new SeckillDTO();
			seckillDTO.setSeckillId(seckillId);
			seckillDTO.setUserName(userName);
			seckillService.seckill(seckillDTO);
			BblockResponse response = new BblockResponse();
			response.setCode(Constants.RESPONSE_CODE_OK);
			response.setMessage("成功");
			return response;
		} catch (Exception e) {
			return new BblockResponse(Constants.RESPONSE_CODE_ERROR, e.toString());
		}

	}
	
}
